package com.lancoo.taskerdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.lancoo.tasker.adapter.BaseRecyclerItem;
import com.lancoo.tasker.adapter.BaseRecyclerViewAdapter;
import com.lancoo.tasker.adapter.RecycleViewDivider;
import com.lancoo.tasker.adapter.SingleItemClickListener;
import com.lancoo.taskerdemo.mode.News;
import com.lancoo.taskerdemo.item.NewsItem;
import com.lancoo.taskerdemo.mode.NewsResponse;
import com.lancoo.taskerdemo.network.ApiFunc2;
import com.lancoo.taskerdemo.network.ApiResponse;
import com.lancoo.taskerdemo.network.NetWork;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by gongwq on 2017/6/19 0019.
 */

public class AdapterDemoActivity extends Activity implements View.OnClickListener {

    String APPKEY = "f020d85d0b9f3bb4923f02c6f02fad46";
    RecyclerView recyclerView;

    private BaseRecyclerViewAdapter<News> adapter = new BaseRecyclerViewAdapter<News>(null) {
        @NonNull
        @Override
        public BaseRecyclerItem<? extends News> createItem() {
            return new NewsItem(getApplication());
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo_adpter);
        recyclerView = (RecyclerView) findViewById(R.id.rv_adapter_demo);
        findViewById(R.id.load_news_btn).setOnClickListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new RecycleViewDivider(AdapterDemoActivity.this,LinearLayoutManager.VERTICAL,3,getResources().getColor(R.color.divider_dark)));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.load_news_btn:

                NetWork.getNewsApi().getNews("guonei", APPKEY)
                        .map(new ApiFunc2<NewsResponse>() {
                            @Override
                            public NewsResponse call(ApiResponse<NewsResponse> response) {
                                Log.e("call---->", "response" + response.error_code);
                                return super.call(response);
                            }
                        })
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeOn(Schedulers.io())
                        .subscribe(new Observer<NewsResponse>() {
                            @Override
                            public void onCompleted() {
                            }

                            @Override
                            public void onError(Throwable e) {
                                e.printStackTrace();
                            }

                            @Override
                            public void onNext(final NewsResponse newsResponse) {
                                Log.e("onNext---->", newsResponse.data.size() + "");
                                adapter.setData(newsResponse.data);
                                adapter.setItemClickListener(new SingleItemClickListener() {
                                    @Override
                                    public void onItemOnClick(View view, int postion) {
                                        Intent intent = new Intent(AdapterDemoActivity.this, NewsDetailActivity.class);
                                        intent.putExtra("news_url",newsResponse.data.get(postion).getUrl());
                                        startActivity(intent);
                                    }
                                });
                            }
                        });


                break;
        }
    }
}
